package com.qdlc.p2p.manage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.ScoreLotteryService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.exception.ScoreException;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.dal.dto.ScoreLottery;
import com.qdlc.p2p.dal.model.ScoreLotteryModel;

/**
 * 积分抽奖奖项管理
 * @author xxx
 * @version 2.0
 * @date 2015年6月15日 下午6:03:16
 */
public class ManageScoreLotteryAction extends BaseAction<ScoreLottery> implements ModelDriven<ScoreLottery> {

	@Resource
	private ScoreLotteryService scoreLotteryService;

	private Map<String, Object> data;

	/**
	 * 奖项管理
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/score/lottery/manage")
	public String manage() throws Exception {
		return "manage";
	}

	/**
	 * 奖项列表
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/score/lottery/list")
	public void list() throws Exception {
		List<ScoreLottery> list = scoreLotteryService.getAllLottery();
		data = new HashMap<String, Object>();
		data.put("total", 1);
		data.put("rows", list);
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 添加奖项页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/score/lottery/append")
	public String append() throws Exception {
		return "append";
	}
	
	/**
	 * 添加奖项
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/score/lottery/doAppend")
	public void doAppend() throws Exception {
		double totalRate = scoreLotteryService.getTotalRate();
		if (BigDecimalUtil.add(totalRate, model.getRate()) > 100) {
			throw new ScoreException("启用的奖项，奖项中奖总概率不能大于100%", BusinessException.TYPE_JSON);
		}
		scoreLotteryService.save(model);
		printSuccess();
	}
	
	/**
	 * 修改奖项页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/score/lottery/modify")
	public String modify() throws Exception {
		ScoreLottery scoreLottery = scoreLotteryService.findById(model.getId());
		request.setAttribute("scoreLottery", scoreLottery);
		return "modify";
	}
	
	/**
	 * 修改奖项
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/score/lottery/doModify")
	public void doModify() throws Exception {
		ScoreLottery scoreLottery = scoreLotteryService.findById(model.getId());
		double totalRate = scoreLotteryService.getTotalRate();
		if (BigDecimalUtil.add(totalRate, model.getRate(), -scoreLottery.getRate()) > 100) {
			throw new ScoreException("启用的奖项，奖项中奖总概率不能大于100%", BusinessException.TYPE_JSON);
		}
		model.setAddTime(scoreLottery.getAddTime());
		scoreLotteryService.update(model);
		printSuccess();
	}
	
	/**
	 * 禁用奖项
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/score/lottery/disable")
	public void disable() throws Exception {
		ScoreLottery scoreLottery = scoreLotteryService.findById(model.getId());
		scoreLottery.setStatus(ScoreLotteryModel.STATUS_DISABLE);
		scoreLotteryService.update(scoreLottery);
		printSuccess();
	}
	
	/**
	 * 启用奖项
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/score/lottery/enable")
	public void enable() throws Exception {
		ScoreLottery scoreLottery = scoreLotteryService.findById(model.getId());
		double totalRate = scoreLotteryService.getTotalRate();
		if (BigDecimalUtil.add(totalRate, scoreLottery.getRate()) > 100) {
			throw new ScoreException("启用的奖项，中奖率总和不能大于100%", BusinessException.TYPE_JSON);
		}
		scoreLottery.setStatus(ScoreLotteryModel.STATUS_ENABLE);
		scoreLotteryService.update(scoreLottery);
		printSuccess();
	}
}
